package cn.zurish.cloud.security;

import org.springframework.expression.EvaluationContext;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;

import java.time.LocalTime;

public class SpELTimeCheck {
    public static void main(String[] args) {
        ExpressionParser parser = new SpelExpressionParser();
        EvaluationContext context = new StandardEvaluationContext();

        // 设置当前时间、开始时间和结束时间到上下文
        context.setVariable("now", LocalTime.now());
        context.setVariable("start", LocalTime.of(9, 0));
        context.setVariable("end", LocalTime.of(17, 0));

        // 使用变量进行比较
        String expression = "#now.isAfter(#start) && #now.isBefore(#end)";
        Boolean result = parser.parseExpression(expression).getValue(context, Boolean.class);

        System.out.println("Current Time: " + LocalTime.now());
        System.out.println("Is between 09:00 and 17:00    " + (Boolean.TRUE.equals(result) ? "Yes" : "No"));
    }
}
